package com.whlilong.jxc.editor.support;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;

import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.listener.ITextKeyChangeListener;
import com.whlilong.jxc.model.Keshang;

public class DistrictEdtiorSupport extends EditingSupport implements
		ITextKeyChangeListener {
	/**
	 * 当前第几列
	 */
	private int currentColumn;

	private boolean isFirst = true;

	private CellEditor editor;

	public DistrictEdtiorSupport(TableViewer viewer, int column,
			IEditorListener listener) {
		super(viewer);
		this.currentColumn = column;
		switch (column) {
		case 2:
		case 3:
			editor = new DistrictTextKeyCellEditor(viewer.getTable(), listener,
					column, this);
			break;
		default:
			editor = null;
		}
	}

	@Override
	protected boolean canEdit(Object element) {
		return (currentColumn != 1);
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		Keshang obj = (Keshang) element;
		switch (currentColumn) {
		case 2:
			String id = obj.getKeshangid();
			if (isFirst) {
				((DistrictTextKeyCellEditor) editor).setOldString(id);
				isFirst = false;
			}
			return id;
		case 3:
			return obj.getKeshangarea();
		}
		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		Keshang obj = (Keshang) element;
		switch (currentColumn) {
		case 2:
			obj.setKeshangid(String.valueOf(value));
			break;
		case 3:
			obj.setKeshangarea(String.valueOf(value));
			break;
		}
		getViewer().update(element, null);
	}

	@Override
	public void changeEvent() {
		if (editor instanceof DistrictTextKeyCellEditor)
			((DistrictTextKeyCellEditor) editor).releaseKey();
	}

	@Override
	public void modify() {
		isFirst = true;
	}

}
